<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品编码" prop="proCode">
        <el-input v-model="queryParams.proCode" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="proName">
        <el-input v-model="queryParams.proName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品类型" prop="proType">
        <el-select v-model="queryParams.proType" placeholder="请选择产品类型" clearable>
          <el-option v-for="dict in dict.type.mes_pro_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable v-model="queryParams.createTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker clearable v-model="queryParams.updateTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 产品信息操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['mes:proinfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['mes:proinfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mes:proinfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mes:proinfo:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>




    <!-- 产品列表 -->
    <el-table v-loading="loading" :data="proinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品编码" align="center" prop="proCode" />
      <el-table-column label="产品名称" align="center" prop="proName" />
      <el-table-column label="成本" align="center" prop="cost">
        <template slot-scope="scope">
          {{ scope.row.cost==null?'0.00元':(scope.row.cost).toFixed(2) + '元' }}
        </template>
      </el-table-column>
      <el-table-column label="定价" align="center" prop="price">
        <template slot-scope="scope">
          {{ scope.row.price==null?'0.00元':(scope.row.price).toFixed(2) + '元' }}
        </template>
      </el-table-column>
      <el-table-column label="产品类型" align="center" prop="proType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_pro_type" :value="scope.row.proType" />
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="工艺" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.tecStatus != 0" type="success">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="teccheck(scope.row)"
              v-hasPermi="['mes:proinfo:edit']">查看</el-button>
          </el-tag>
          <el-tag v-else type="danger">
            <el-button size="mini" style="color: brown;" type="text" icon="el-icon-edit" @click="addNewTec(scope.row)"
              v-hasPermi="['mes:proinfo:edit']">待完成</el-button>
          </el-tag>

        </template>
      </el-table-column>
      <el-table-column label="BOM" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.bomStatus != 0" type="success">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="bomcheck(scope.row)"
              v-hasPermi="['mes:proinfo:edit']">查看</el-button>
          </el-tag>
          <el-tag v-else type="danger">
            <el-button size="mini" type="text" style="color: brown;" icon="el-icon-edit" @click="bomtodo(scope.row)"
              v-hasPermi="['mes:proinfo:edit']">待完成</el-button>
          </el-tag>

        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:proinfo:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mes:proinfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改产品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品编码" prop="proCode">
          <el-input v-model="form.proCode" placeholder="请输入产品编码" :disabled="exitdisable" />
        </el-form-item>
        <el-form-item label="产品名称" prop="proName">
          <el-input v-model="form.proName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品类型" prop="proType">
          <el-select v-model="form.proType" placeholder="请选择产品类型">
            <el-option v-for="dict in dict.type.mes_pro_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="定价" prop="price">
          <el-input-number v-model="form.price" :precision="2" :step="1" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>





    <!-- 产品工艺抽屉 -->
    <el-drawer title="产品工艺" :visible.sync="tecOpen" v-if="tecOpen" direction="rtl">
      <el-card style="margin:5px">
        <div slot="header" class="clearfix">
          <el-tag style="margin-right:10px">产品编码</el-tag><span style="font-size: 15px;font-weight: 700;">{{
            nowProData.proCode }}</span>
        </div>
        <div style="display: inline-block;">
          <div style="display: block;margin-bottom: 5px;">
            <el-tag style="margin-right:10px">产品名称</el-tag><span style="font-size: 15px;font-weight: 700;">{{
              nowProData.proName }}</span>
          </div>
          <el-tag style="margin-right:10px">产品类型</el-tag><span style="font-size: 15px;font-weight: 700;">{{
            nowProData.proType == 1 ? "成品" : "半成品" }}</span>
        </div>
        <div style="display: inline-block;float: right;">
          <img src="@/assets/images/chanpin.png" style="height: 55px;">
        </div>
      </el-card>
      <el-divider></el-divider>
      <div style="text-align: center;margin-bottom: 20px;">
        <label style="background-color: rgba(119, 211, 214, 0.623);padding: 10px;border-radius: 10px;">
          <template v-if="!addTecMode">工艺编码:{{ proTecData.tecCode }}</template>
          <template v-else>工艺编码: <el-input size="mini" style="display: inline-block;width: 100px;"
              v-model="newTec.tecCode" placeholder="请输入内容"></el-input></template>
        </label>

        <div v-if="tecIsmodify"
          style="display: inline-block;background-color: rgba(255, 156, 120, 0.76);padding: 5px;border-radius: 10px;margin-left: 10px;">
          <label>选择工序: </label>
          <el-select style="width: 150px;" size="mini" v-model="showValue" filterable placeholder="请选择"
            @change="proceChange">
            <el-option v-for="item in proceOptions" :key="item.id" :label="item.proceName" :value="item.id">
            </el-option>
          </el-select>
        </div>
      </div>

      <!-- 不处于新增模式时显示 -->
      <draggable v-if="!addTecMode" :list="proTecData.proceInfos" :disabled="!tecIsmodify">
        <transition-group>
          <el-card :id="cardColour()" style="position: relative;border-radius: 15px;margin:10px;" shadow="always"
            v-for="(item, index) in proTecData.proceInfos" :key="item.id">
            <div style="display: inline-block;">
              <div style="margin-bottom: 10px;">
                <span
                  style="background-color: #a8a8a894;border-radius: 7px;padding: 5px;margin-right: 5px;">工序顺序</span><label>{{
                    index }}</label>
              </div>
              <div>
                <span
                  style="background-color: #a8a8a894;border-radius: 7px;padding: 5px;margin-right: 5px;">工序名称</span><label>{{
                    item.proceName }}</label>
              </div>
            </div>
            <div style="display: inline-block;height: 100%;">
              <div
                style="display: flex;text-align: center;position: absolute;right: 180px;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;">
                <button id="shebeibtn" @click="proMtrSet(index)">原料查看</button>
              </div>
              <div
                style="display: flex;text-align: center;position: absolute;right: 100px;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;">
                <button id="shebeibtn" @click="proDeviceSet(index)">设备查看</button>
              </div>
            </div>
            <div
              style="display: flex;text-align: center;position: absolute;right: 0;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;"
              v-if="tecIsmodify">
              <el-button id="daohuobtn" type="danger" icon="el-icon-delete" circle @click="proceDel(index)"></el-button>
            </div>
          </el-card>
        </transition-group>
      </draggable>


      <!-- 新增模式显示 -->
      <draggable v-else :list="newTec.proceInfos" :disabled="!tecIsmodify">
        <transition-group>
          <el-card :id="cardColour()" style="position: relative;border-radius: 15px;margin:10px;" shadow="always"
            v-for="(item, index) in newTec.proceInfos" :key="item.id">
            <div style="display: inline-block;">
              <label style="display: block;">工序顺序：{{ index }}</label>
              <label style="display: block;">工序名称：{{ item.proceName }}</label>
            </div>
            <div style="display: inline-block;height: 100%;">
              <div
                style="display: flex;text-align: center;position: absolute;right: 180px;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;">
                <button id="shebeibtn" @click="proMtrSet(index)">原料查看</button>
              </div>
              <div
                style="display: flex;text-align: center;position: absolute;right: 100px;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;">
                <button id="shebeibtn" @click="proDeviceSet(index)">设备查看</button>
              </div>
            </div>
            <div
              style="display: flex;text-align: center;position: absolute;right: 0;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;"
              v-if="tecIsmodify">
              <el-button id="daohuobtn" type="danger" icon="el-icon-delete" circle @click="proceDel(index)"></el-button>
            </div>
          </el-card>
        </transition-group>
      </draggable>
      <div style="text-align: center;">
        <el-button v-if="!tecIsmodify" type="warning" plain @click="tecIsmodify = true">编 辑</el-button>
        <el-button v-else type="primary" plain @click="saveTec">保 存</el-button>
        <el-button type="primary" plain @click="tecOpen = false">取 消</el-button>
      </div>

    </el-drawer>


    <!-- 工序原料对话框 -->
    <el-dialog title="工序原料" :visible.sync="tecMtrOpen" v-if="tecMtrOpen" width="500px" append-to-body>
      <div style="text-align: center;">
        <div v-if="tecIsmodify"
          style="display: inline-block;background-color: rgba(255, 156, 120, 0.76);padding: 5px;border-radius: 10px;margin-left: 10px;">
          <label>选择原料:</label>
          <el-select size="mini" v-model="showValue" filterable placeholder="请选择" @change="mtrChange">
            <el-option v-for="item in proceMtrOptions" :key="item.mtrId" :label="item.mtrName" :value="item.mtrId">
            </el-option>
          </el-select>
        </div>
      </div>

      <!-- 非新增模式 -->
      <div v-if="!addTecMode">
        <transition name="el-fade-in-linear" v-for="(item, index) in proTecData.proceInfos[nowProceIndex].proceMtrs"
          :key="item.id">
          <transition name="el-fade-in">
            <el-card
              style="position: relative;border-radius: 15px;margin-top: 5px;margin-bottom: 5px;background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);"
              shadow="always">
              <div style="display: inline-block;">
                <div style="margin-bottom: 10px;">
                  <span
                    style="background-color: #a8a8a894;border-radius: 7px;padding: 5px;margin-right: 5px;">原料名称</span><label
                    style="font-size: 15px;">{{ item.mtrName }}</label>
                </div>
                <div>
                  <span
                    style="background-color: #a8a8a894;border-radius: 7px;padding: 5px;margin-right: 5px;">原料编码</span><label
                    style="font-size: 15px;font-weight: 700;">{{ item.mtrCode }}</label>
                </div>
              </div>
              <div
                style="display: flex;text-align: center;position: absolute;right: 160px;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;">

                <label v-if="!tecIsmodify">原料数量:{{ item.mtrQuantity }}</label>
                <label v-else>原料数量:<el-input-number size="mini" v-model="item.mtrQuantity"></el-input-number></label>
              </div>
              <div
                style="display: flex;text-align: center;position: absolute;right: 0;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;"
                v-if="tecIsmodify">
                <el-button id="daohuobtn" type="danger" icon="el-icon-delete" circle
                  @click="proceMtrDel(index)"></el-button>
              </div>
            </el-card>
          </transition>
        </transition>
      </div>


      <div v-else>
        <transition name="el-fade-in-linear" v-for="(item, index) in newTec.proceInfos[nowProceIndex].proceMtrs"
          :key="item.id">
          <transition name="el-fade-in">
            <el-card
              style="position: relative;border-radius: 15px;margin-top: 5px;margin-bottom: 5px;background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);"
              shadow="always">
              <div style="display: inline-block;">
                <label style="display: block;font-weight: 400;font-style:oblique;">原料名称:<span
                    style="font-size: 15px;font-weight: 700;">{{ item.mtrName }}</span></label>
                <label style="display: block;font-weight: 400;font-style:oblique;">原料编码:<span
                    style="font-size: 15px;font-weight: 700;">{{ item.mtrCode }}</span></label>
              </div>
              <div
                style="display: flex;text-align: center;position: absolute;right: 160px;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;">
                <label v-if="!tecIsmodify">原料数量:{{ item.mtrQuantity }}</label>
                <label v-else>原料数量:<el-input-number size="mini" v-model="item.mtrQuantity"></el-input-number></label>
              </div>
              <div
                style="display: flex;text-align: center;position: absolute;right: 0;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;"
                v-if="tecIsmodify">
                <el-button id="daohuobtn" type="danger" icon="el-icon-delete" circle
                  @click="proceMtrDel(index)"></el-button>
              </div>
            </el-card>
          </transition>
        </transition>
      </div>




      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="tecMtrOpen = false">确 定</el-button>
      </div>
    </el-dialog>






    <!-- 工序设备对话框 -->
    <el-dialog title="工序设备" :visible.sync="tecDeviceOpen" v-if="tecDeviceOpen" width="500px" append-to-body>
      <div style="text-align: center;">
        <div v-if="tecIsmodify"
          style="display: inline-block;background-color: rgba(255, 156, 120, 0.76);padding: 5px;border-radius: 10px;margin-left: 10px;">
          <label>选择设备:</label>
          <el-select size="mini" v-model="showValue" filterable placeholder="请选择" @change="deviceChange">
            <el-option v-for="item in proceDeviceOptions" :key="item.id" :label="item.deviceName"
              :value="item.deviceCode">
            </el-option>
          </el-select>
        </div>
      </div>

      <div v-if="!addTecMode">
        <transition name="el-fade-in-linear" v-for="(item, index) in proTecData.proceInfos[nowProceIndex].deviceInfos"
          :key="item.id">
          <transition name="el-fade-in">
            <el-card
              style="position: relative;border-radius: 15px;margin-top: 5px;margin-bottom: 5px;background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);"
              shadow="always">
              <div style="display: inline-block;">
                <div style="margin-bottom: 10px;">
                  <span
                    style="background-color: #a8a8a894;border-radius: 7px;padding: 5px;margin-right: 5px;">设备编码</span><label>{{
                      item.deviceCode }}</label>
                </div>
                <div>
                  <span
                    style="background-color: #a8a8a894;border-radius: 7px;padding: 5px;margin-right: 5px;">设备名称</span><label>{{
                      item.deviceName }}</label>
                </div>
              </div>

              <div
                style="position: relative;;display: flex;text-align: center;position: absolute;right: 0;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;"
                v-if="tecIsmodify">
                <el-button id="daohuobtn" type="danger" icon="el-icon-delete" circle
                  @click="proceDeviceDel(index)"></el-button>
              </div>
            </el-card>
          </transition>
        </transition>
      </div>



      <div v-else>
        <transition name="el-fade-in-linear" v-for="(item, index) in newTec.proceInfos[nowProceIndex].deviceInfos"
          :key="item.id">
          <transition name="el-fade-in">
            <el-card
              style="position: relative;border-radius: 15px;margin-top: 5px;margin-bottom: 5px;background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);"
              shadow="always">
              <div style="display: inline-block;">
                <label style="display: block;">设备编码:{{ item.deviceCode }}</label>
                <label style="display: block;">设备名称:{{ item.deviceName }}</label>
              </div>

              <div
                style="position: relative;;display: flex;text-align: center;position: absolute;right: 0;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;"
                v-if="tecIsmodify">
                <el-button id="daohuobtn" type="danger" icon="el-icon-delete" circle
                  @click="proceDeviceDel(index)"></el-button>
              </div>
            </el-card>
          </transition>
        </transition>
      </div>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="tecDeviceOpen = false">确 定</el-button>
      </div>
    </el-dialog>




    <!-- 产品BOM抽屉 -->
    <el-drawer title="产品BOM" :visible.sync="bomOpen" v-if="bomOpen" direction="rtl">
      <el-card style="margin:5px">
        <div slot="header" class="clearfix">
          <el-tag style="margin-right:10px">产品编码</el-tag><span style="font-size: 15px;font-weight: 700;">{{
            nowProData.proCode }}</span>
        </div>
        <div style="display: inline-block;">
          <div style="display: block;margin-bottom: 5px;">
            <el-tag style="margin-right:10px">产品名称</el-tag><span style="font-size: 15px;font-weight: 700;">{{
              nowProData.proName }}</span>
          </div>
          <el-tag style="margin-right:10px">产品类型</el-tag><span style="font-size: 15px;font-weight: 700;">{{
            nowProData.proType == 1 ? "成品" : "半成品" }}</span>
        </div>
        <div style="display: inline-block;float: right;">
          <img src="@/assets/images/chanpin.png" style="height: 55px;">
        </div>
      </el-card>
      <el-divider></el-divider>
      <div style="text-align: center;margin-bottom: 10px;">
        <label style="background-color: rgba(119, 211, 214, 0.623);padding: 10px;border-radius: 10px;">
          <template v-if="nowProData.bomStatus != 0">BOM编码:{{ proBomData.bomCode }}</template>
          <template v-else>BOM编码: <el-input size="mini" style="display: inline-block;width: 100px;"
              v-model="proBomData.bomCode" placeholder="请输入内容"></el-input></template>
        </label>
        <div v-if="bomIsmodify"
          style="display: inline-block;background-color: rgba(255, 156, 120, 0.76);padding: 5px;border-radius: 10px;margin-left: 10px;">
          <label>选择原料: </label>
          <el-select style="width: 150px;" size="mini" v-model="showValue" filterable placeholder="请选择"
            @change="bomMtrChange">
            <el-option v-for="item in bomMtrOptions" :key="item.id" :label="item.mtrName" :value="item.id">
            </el-option>
          </el-select>
        </div>

      </div>
      <div>
        <el-card style="margin: 10px;border-radius: 15px;position: relative;" shadow="always"
          v-for="(item, index) in proBomData.mtrs" :key="item.id">
          <div class="bomMtrCardBigDiv" style="display: flex;text-align: center;width: 100%;transition: all 0.3s">
            <div id="bomMtrCardDiv">
              <span id="topspan">物料名称</span>
              <span id="bottomspan">{{ item.mtrName }}</span>
            </div>
            <div id="bomMtrCardDiv">
              <span id="topspan">物料编码</span>
              <span id="bottomspan">{{ item.mtrCode }}</span>
            </div>
            <div id="bomMtrCardDiv">
              <span v-if="!bomIsmodify" id="topspan">数量</span>
              <span v-if="!bomIsmodify" id="bottomspan">{{ item.mtrQuantity }}</span>
              <el-input-number v-else style="position: relative;top:15px" size="mini"
                v-model="item.mtrQuantity"></el-input-number>
            </div>
          </div>
          <div
            style="position: relative;;display: flex;text-align: center;position: absolute;right: 0;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;"
            v-if="bomIsmodify">
            <el-button id="daohuobtn" type="danger" icon="el-icon-delete" circle @click="bomMtrDel(index)"></el-button>
          </div>
        </el-card>
      </div>
      <div style="text-align: center;">
        <el-button v-if="!bomIsmodify" type="warning" plain @click="bomModify()">编 辑</el-button>
        <el-button v-else type="primary" plain @click="saveBom">保 存</el-button>
        <el-button type="primary" plain @click="bomOpen = false">取 消</el-button>
      </div>
    </el-drawer>

  </div>
</template>

<script>

import { listProinfo, getProinfo, delProinfo, addProinfo, updateProinfo, getProTec, getBomAndMtrByProId, editProTecAndMtrs, editProBomAndMtrs } from "@/api/mes/proinfo";
import draggable from 'vuedraggable';
import { listProce } from "@/api/mes/proce"
import { listDevice } from "@/api/mes/device"
import { listMtrinfo } from "@/api/mes/mtrinfo"
import { listTec, getTec, delTec, addTec, updateTec } from "@/api/mes/tec";
export default {
  name: "Proinfo",
  dicts: ['mes_pro_type'],
  components: {
    draggable,
  },
  data() {
    return {
      //BOM原料下拉框数据
      bomMtrOptions: [],
      //新增工艺模式
      addTecMode: false,
      //下拉框绑定值(展示用)
      showValue: "",
      //工艺下拉框数据
      proceOptions: [],
      //工艺设备下拉框数据
      proceDeviceOptions: [],
      //工艺原料下拉框数据
      proceMtrOptions: [],
      //当前点击的工序下标
      nowProceIndex: "",
      //是否处于工艺编辑状态
      tecIsmodify: false,
      //是否处于bom编辑状态
      bomIsmodify: false,
      //工序设备对话框
      tecDeviceOpen: false,
      //工序原料对话框
      tecMtrOpen: false,
      //当前产品的工艺信息
      proTecData: [],
      //当前产品的BOM信息
      proBomData: [],
      //当前点击的产品信息
      nowProData: "",
      //工艺抽屉
      tecOpen: false,
      //BOM抽屉
      bomOpen: false,
      //产品表单能否修改
      exitdisable: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proCode: null,
        proName: null,
        proType: null,
        createTime: null,
        updateTime: null,
      },
      // 表单参数
      form: {},
      //待完成工艺
      newTec: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "产品id不能为空", trigger: "blur" }
        ],
        tecId: [
          { required: true, message: "工艺id不能为空", trigger: "blur" }
        ],
        bomId: [
          { required: true, message: "BOMid不能为空", trigger: "blur" }
        ]
      }
    };
  },
  watch: {

  },
  created() {
    this.getList();
  },
  methods: {
    //bom编辑按钮
    bomModify() {
      this.bomIsmodify = true
      let ss = document.getElementsByClassName('bomMtrCardBigDiv')
      // 目前只找到使用这种方法批量改变样式
      for (let i = 0; i < ss.length; i++) {
        ss[i].style.width = "80%"
      }
    },
    //bom待完成
    bomtodo(row) {
      this.reset();
      this.nowProData = row
      this.bomIsmodify = true
      this.bomOpen = true
      this.proBomData.proId = row.id
    },
    //bom修改保存
    saveBom() {
      if (this.proBomData.bomCode == null || this.proBomData.mtrs.length == 0) {
        return this.$modal.msgError("BOM编码和原料列表不能为空");
      }
      if (this.nowProData.bomStatus == 0) {
        editProBomAndMtrs(this.proBomData).then(res => {
          this.$modal.msgSuccess("添加成功");
          this.bomIsmodify = false;
          getBomAndMtrByProId({ id: this.nowProData.id }).then(res => {
            this.proBomData = res.data
          })
          this.getList();
          this.bomOpen = false
        })
      } else {
        editProBomAndMtrs(this.proBomData).then(res => {
          this.$modal.msgSuccess("修改成功");
          this.bomIsmodify = false;
          getBomAndMtrByProId({ id: this.nowProData.id }).then(res => {
            this.proBomData = res.data
          })
        })
      }
      let ss = document.getElementsByClassName('bomMtrCardBigDiv')
      // 目前只找到使用这种方法批量改变样式
      for (let i = 0; i < ss.length; i++) {
        ss[i].style.width = "100%"
      }
    },
    //bom原料删除
    bomMtrDel(index) {
      this.proBomData.mtrs.splice(index, 1)
    },
    //bom原料下拉框改变
    bomMtrChange(option) {
      if (this.proBomData.mtrs.findIndex(e => e.id == option) != -1) {
        this.$modal.msgError("无法添加重复的原料");
      } else {
        this.proBomData.mtrs.push(this.bomMtrOptions.find(e => e.id == option))
      }
      this.showValue = ""
      setTimeout(function () {
        // 目前只找到使用这种方法批量改变样式
        let ss = document.getElementsByClassName('bomMtrCardBigDiv')
        for (let i = 0; i < ss.length; i++) {
          ss[i].style.width = "80%"
        }
      }, 100)

    },
    //BOM查看按钮
    bomcheck(row) {
      this.nowProData = row
      this.bomIsmodify = false

      getBomAndMtrByProId({ id: row.id }).then(res => {
        this.proBomData = res.data
        this.bomOpen = true
      })
    },
    //工序卡片颜色控制
    cardColour() {
      if (this.tecIsmodify) {
        return "ismodify"
      } else {
        return "nomodify"
      }
    },
    //待完成工序按钮
    addNewTec(row) {
      this.reset()
      this.nowProData = row
      this.addTecMode = true;
      this.tecIsmodify = true;
      this.tecOpen = true;
    },
    //删除工序设备
    proceDeviceDel(index) {
      if (this.addTecMode) {
        this.newTec.proceInfos[this.nowProceIndex].deviceInfos.splice(index, 1)
      } else {
        this.proTecData.proceInfos[this.nowProceIndex].deviceInfos.splice(index, 1)
      }

    },
    //删除工序原料
    proceMtrDel(index) {
      if (this.addTecMode) {
        this.newTec.proceInfos[this.nowProceIndex].proceMtrs.splice(index, 1)
      } else {
        this.proTecData.proceInfos[this.nowProceIndex].proceMtrs.splice(index, 1)
      }

    },
    //删除工序
    proceDel(index) {
      if (this.addTecMode) {
        this.newTec.proceInfos.splice(index, 1)
      } else {
        this.proTecData.proceInfos.splice(index, 1)
      }

    },
    //工序设备下拉框改变
    deviceChange(option) {
      this.showValue = ""
      if (this.addTecMode) {
        if (this.newTec.proceInfos[this.nowProceIndex].deviceInfos.findIndex(e => e.deviceCode == option) != -1) {
          this.$modal.msgError("无法添加重复的设备");
        } else {
          this.newTec.proceInfos[this.nowProceIndex].deviceInfos.push(this.proceDeviceOptions.find(e => e.deviceCode == option))
        }
      } else {
        if (this.proTecData.proceInfos[this.nowProceIndex].deviceInfos.findIndex(e => e.deviceCode == option) != -1) {
          this.$modal.msgError("无法添加重复的设备");
        } else {
          this.proTecData.proceInfos[this.nowProceIndex].deviceInfos.push(this.proceDeviceOptions.find(e => e.deviceCode == option))
        }
      }

    },
    //工序原料下拉框改变
    mtrChange(option) {
      this.showValue = ""
      if (this.addTecMode) {
        if (this.newTec.proceInfos[this.nowProceIndex].proceMtrs.findIndex(e => e.mtrId == option) != -1) {
          this.$modal.msgError("无法添加重复的物料");
        } else {
          this.newTec.proceInfos[this.nowProceIndex].proceMtrs.push(this.proceMtrOptions.find(e => e.mtrId == option))
        }
      } else {
        if (this.proTecData.proceInfos[this.nowProceIndex].proceMtrs.findIndex(e => e.mtrId == option) != -1) {
          this.$modal.msgError("无法添加重复的物料");
        } else {
          this.proTecData.proceInfos[this.nowProceIndex].proceMtrs.push(this.proceMtrOptions.find(e => e.mtrId == option))
        }
      }

    },
    //工艺下拉框改变事件
    proceChange(option) {
      if (this.addTecMode) {
        if (this.newTec.proceInfos.findIndex(e => e.id == option) != -1) {
          this.$modal.msgError("无法添加重复的工序");
        } else {
          let ss = this.proceOptions.find(e => e.id == option)
          ss.deviceInfos = []
          ss.proceMtrs = []
          this.newTec.proceInfos.push(ss)
        }
      } else {
        if (this.proTecData.proceInfos.findIndex(e => e.id == option) != -1) {
          this.$modal.msgError("无法添加重复的工序");
        } else {
          let ss = this.proceOptions.find(e => e.id == option)
          ss.deviceInfos = []
          ss.proceMtrs = []
          this.proTecData.proceInfos.push(ss)
          // this.form.proceInfos[this.nowProceindex].proceMtrs.push(this.proceMtrOptions.find(e => e.mtrId == option))
        }
      }
      this.showValue = ""
    },
    //工序设备设置按钮
    proDeviceSet(index) {

      this.tecDeviceOpen = true;
      this.nowProceIndex = index
      listDevice().then(res => {
        this.proceDeviceOptions = res.rows
      })
    },
    //工序原料设置按钮
    proMtrSet(index) {

      this.tecMtrOpen = true;
      this.nowProceIndex = index
      listMtrinfo().then(res => {
        this.proceMtrOptions = JSON.parse(JSON.stringify(res.rows).replace(/id/g, 'mtrId'))
      })
    },
    //工艺查看按钮
    teccheck(row) {
      //关闭编辑模式
      this.tecIsmodify = false;
      //关闭新增模式
      this.addTecMode = false;
      this.nowProData = row
      getProTec({ proId: row.id }).then(res => {
        this.proTecData = res.data
        this.tecOpen = true;
      })
    },
    // 工艺确认保存按钮
    saveTec() {
      if (this.addTecMode) {
        if (this.newTec.tecCode == null || this.newTec.proceInfos.length == 0) {
          return this.$modal.msgError("工序和工艺编码不能为空");
        }
        this.newTec.proId = this.nowProData.id
        this.newTec.proceInfos.forEach((item, index) => {
          item.proceSeque = index
        })
        if (this.newTec.id == null) {
          addTec(this.newTec).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.addTecMode = false;
            this.getList();
            this.tecOpen = false;
          });
        }
      } else {
        this.proTecData.proceInfos.forEach((item, index) => {
          item.proceSeque = index
        })
        if (this.proTecData.id != null) {
          editProTecAndMtrs(this.proTecData).then(response => {
            this.$modal.msgSuccess("修改成功");
            getProTec({ proId: this.nowProData.id }).then(res => {
              this.proTecData = res.data
            })
            this.tecIsmodify = false
            this.getList();
          });
        }

      }



    },



    // ——————————————————————————————————————————————————————若依自动生成代码————————————————————————————————————————————————————————
    /** 查询产品信息列表 */
    getList() {
      this.loading = true;
      listProinfo(this.queryParams).then(response => {
        this.proinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      //获取工序下拉框
      listProce().then(res => {
        this.proceOptions = res.rows;
      })
      //获取bom原料下拉框
      listMtrinfo().then(res => {
        this.bomMtrOptions = res.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;

      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        proCode: null,
        proName: null,
        proType: null,
        createTime: null,
        updateTime: null,
        remark: null,
        tecId: null,
        bomId: null,
        price:0,
      };
      this.newTec = {
        id: null,
        //产品id
        proId: null,
        //工艺编码
        tecCode: null,
        //工序集合
        proceInfos: [],
      };
      this.proBomData = {
        bomCode: null,
        mtrs: [],
        proId: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.exitdisable = false;
      this.reset();
      this.open = true;
      this.title = "添加产品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // console.log(row)
      this.exitdisable = true;
      this.reset();
      const id = row.id || this.ids
      getProinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品信息编号为"' + ids + '"的数据项？').then(function () {
        return delProinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/proinfo/export', {
        ...this.queryParams
      }, `proinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
#daohuobtn {
  height: 45px;
  width: 45px;
  position: relative;
  align-items: center;
  justify-content: center;
  transition: width 0.3s, height 0.3s, border-radius 0.3s;
}

#daohuobtn:hover {
  height: 100%;
  width: 100%;
  border-radius: unset;
}

#shebeibtn {
  font-size: 10px;
  font-weight: 700;
  height: 45px;
  width: 45px;
  position: relative;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
  border-radius: 10px;
  border: unset;
  background-color: rgba(158, 190, 199, 0.815);
}

#shebeibtn:hover {
  height: 100%;
  width: 100%;
  border-radius: 5px;
}

/deep/.el-drawer__header {
  margin-bottom: 10px;
}

/deep/ .el-drawer {
  border-radius: 15px;
}

/deep/ .el-card__body {
  padding: 10px;
}

#ismodify {
  background-image: linear-gradient(to top, #c1dfc4 0%, #deecdd 100%);
}

#nomodify {
  background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);

}

#bomMtrCardDiv {
  display: inline-block;
  flex: 1;
  padding-left: 5px;
  padding-right: 5px;
}

#topspan {
  display: block;
  background-color: rgb(250, 217, 144);
  border-top-right-radius: 7px;
  border-top-left-radius: 7px;
  font-style: oblique
}

#bottomspan {
  display: block;
  background-color: rgba(95, 158, 160, 0.582);
  border-bottom-left-radius: 7px;
  border-bottom-right-radius: 7px;
  font-size: 16px;
  font-weight: 600;
  height: 40px;
  padding-top: 6px;
}</style>
